home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / malloc.lha / malloc / ralloc.c < prev    next >
C/C++ Source or Header  |  1993-05-26  |  13KB  |  515 lines

  1. /* Block-relocating memory allocator. 
  2.    Copyright (C) 1993 Free Software Foundation, Inc.
  3.  
  4.  
  5. This file is part of the GNU C Library.
  6.  
  7. The GNU C Library is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU Library General Public License as
  9. published by the Free Software Foundation; either version 2 of the
  10. License, or (at your option) any later version.
  11.  
  12. The GNU C Library is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15. Library General Public License for more details.
  16.  
  17. You should have received a copy of the GNU Library General Public
  18. License along with the GNU C Library; see the file COPYING.LIB.  If
  19. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  20. Cambridge, MA 02139, USA.  */
  21.  
  22. /* NOTES:
  23.  
  24.    Only relocate the blocs neccessary for SIZE in r_alloc_sbrk,
  25.    rather than all of them.  This means allowing for a possible
  26.    hole between the first bloc and the end of malloc storage. */
  27.  
  28. #ifdef emacs
  29.  
  30. #include "config.h"
  31. #include "lisp.h"        /* Needed for VALBITS.  */
  32.  
  33. #undef NULL
  34.  
  35. /* The important properties of this type are that 1) it's a pointer, and
  36.    2) arithmetic on it should work as if the size of the object pointed
  37.    to has a size of 1.  */
  38. #if 0 /* Arithmetic on void* is a GCC extension.  */
  39. #ifdef __STDC__
  40. typedef void *POINTER;
  41. #else
  42.  
  43. #ifdef    HAVE_CONFIG_H
  44. #include "config.h"
  45. #endif
  46.  
  47. typedef char *POINTER;
  48.  
  49. #endif
  50. #endif /* 0 */
  51.  
  52. /* Unconditionally use char * for this.  */
  53. typedef char *POINTER;
  54.  
  55. typedef unsigned long SIZE;
  56.  
  57. /* Declared in dispnew.c, this version doesn't screw up if regions
  58.    overlap.  */
  59. extern void safe_bcopy ();
  60.  
  61. #include "getpagesize.h"
  62.  
  63. #else    /* Not emacs.  */
  64.  
  65. #include <stddef.h>
  66.  
  67. typedef size_t SIZE;
  68. typedef void *POINTER;
  69.  
  70. #include <unistd.h>
  71. #include <malloc.h>
  72. #include <string.h>
  73.  
  74. #define safe_bcopy(x, y, z) memmove (y, x, z)
  75.  
  76. #endif    /* emacs.  */
  77.  
  78. #define NIL ((POINTER) 0)
  79.  
  80. /* A flag to indicate whether we have initialized ralloc yet.  For
  81.    Emacs's sake, please do not make this local to malloc_init; on some
  82.    machines, the dumping procedure makes all static variables
  83.    read-only.  On these machines, the word static is #defined to be
  84.    the empty string, meaning that r_alloc_initialized becomes an
  85.    automatic variable, and loses its value each time Emacs is started up.  */
  86. static int r_alloc_initialized = 0;
  87.  
  88. static void r_alloc_init ();
  89.  
  90. /* Declarations for working with the malloc, ralloc, and system breaks.  */
  91.  
  92. /* Function to set the real break value. */
  93. static POINTER (*real_morecore) ();
  94.  
  95. /* The break value, as seen by malloc (). */
  96. static POINTER virtual_break_value;
  97.  
  98. /* The break value, viewed by the relocatable blocs. */
  99. static POINTER break_value;
  100.  
  101. /* The REAL (i.e., page aligned) break value of the process. */
  102. static POINTER page_break_value;
  103.  
  104. /* This is the size of a page.  We round memory requests to this boundary.  */
  105. static int page_size;
  106.  
  107. /* Whenever we get memory from the system, get this many extra bytes.  This 
  108.    must be a multiple of page_size.  */
  109. static int extra_bytes;
  110.  
  111. /* Macros for rounding.  Note that rounding to any value is possible
  112.    by changing the definition of PAGE. */
  113. #define PAGE (getpagesize ())
  114. #define ALIGNED(addr) (((unsigned int) (addr) & (page_size - 1)) == 0)
  115. #define ROUNDUP(size) (((unsigned int) (size) + page_size - 1) & ~(page_size - 1))
  116. #define ROUND_TO_PAGE(addr) (addr & (~(page_size - 1)))
  117.  
  118. /* Functions to get and return memory from the system.  */
  119.  
  120. /* Obtain SIZE bytes of space.  If enough space is not presently available
  121.    in our process reserve, (i.e., (page_break_value - break_value)),
  122.    this means getting more page-aligned space from the system.
  123.  
  124.    Return non-zero if all went well, or zero if we couldn't allocate
  125.    the memory.  */
  126. static int
  127. obtain (size)
  128.      SIZE size;
  129. {
  130.   SIZE already_available = page_break_value - break_value;
  131.  
  132.   if (already_available < size)
  133.     {
  134.       SIZE get = ROUNDUP (size - already_available);
  135.       /* Get some extra, so we can come here less often.  */
  136.       get += extra_bytes;
  137.  
  138.       if ((*real_morecore) (get) == 0)
  139.     return 0;
  140.  
  141.       page_break_value += get;
  142.     }
  143.  
  144.   break_value += size;
  145.  
  146.   return 1;
  147. }
  148.  
  149. /* Obtain SIZE bytes of space and return a pointer to the new area.
  150.    If we could not allocate the space, return zero.  */
  151.  
  152. static POINTER
  153. get_more_space (size)
  154.      SIZE size;
  155. {
  156.   POINTER ptr = break_value;
  157.   if (obtain (size))
  158.     return ptr;
  159.   else
  160.     return 0;
  161. }
  162.  
  163. /* Note that SIZE bytes of space have been relinquished by the process.
  164.    If SIZE is more than a page, return the space to the system. */
  165.  
  166. static void
  167. relinquish (size)
  168.      SIZE size;
  169. {
  170.   POINTER new_page_break;
  171.   int excess;
  172.  
  173.   break_value -= size;
  174.   new_page_break = (POINTER) ROUNDUP (break_value);
  175.   excess = (char *) page_break_value - (char *) new_page_break;
  176.   
  177.   if (excess > extra_bytes * 2)
  178.     {
  179.       /* Keep extra_bytes worth of empty space.
  180.      And don't free anything unless we can free at least extra_bytes.  */
  181.       if ((*real_morecore) (extra_bytes - excess) == 0)
  182.     abort ();
  183.  
  184.       page_break_value += extra_bytes - excess;
  185.     }
  186.  
  187.   /* Zero the space from the end of the "official" break to the actual
  188.      break, so that bugs show up faster.  */
  189.   bzero (break_value, ((char *) page_break_value - (char *) break_value));
  190. }
  191.  
  192. /* The meat - allocating, freeing, and relocating blocs.  */
  193.  
  194. /* These structures are allocated in the malloc arena.
  195.    The linked list is kept in order of increasing '.data' members.
  196.    The data blocks abut each other; if b->next is non-nil, then
  197.    b->data + b->size == b->next->data.  */
  198. typedef struct bp
  199. {
  200.   struct bp *next;
  201.   struct bp *prev;
  202.   POINTER *variable;
  203.   POINTER data;
  204.   SIZE size;
  205. } *bloc_ptr;
  206.  
  207. #define NIL_BLOC ((bloc_ptr) 0)
  208. #define BLOC_PTR_SIZE (sizeof (struct bp))
  209.  
  210. /* Head and tail of the list of relocatable blocs. */
  211. static bloc_ptr first_bloc, last_bloc;
  212.  
  213. /* Find the bloc referenced by the address in PTR.  Returns a pointer
  214.    to that block. */
  215.  
  216. static bloc_ptr
  217. find_bloc (ptr)
  218.      POINTER *ptr;
  219. {
  220.   register bloc_ptr p = first_bloc;
  221.  
  222.   while (p != NIL_BLOC)
  223.     {
  224.       if (p->variable == ptr && p->data == *ptr)
  225.     return p;
  226.  
  227.       p = p->next;
  228.     }
  229.  
  230.   return p;
  231. }
  232.  
  233. /* Allocate a bloc of SIZE bytes and append it to the chain of blocs.
  234.    Returns a pointer to the new bloc, or zero if we couldn't allocate
  235.    memory for the new block.  */
  236.  
  237. static bloc_ptr
  238. get_bloc (size)
  239.      SIZE size;
  240. {
  241.   register bloc_ptr new_bloc;
  242.  
  243.   if (! (new_bloc = (bloc_ptr) malloc (BLOC_PTR_SIZE))
  244.       || ! (new_bloc->data = get_more_space (size)))
  245.     {
  246.       if (new_bloc)
  247.     free (new_bloc);
  248.  
  249.       return 0;
  250.     }
  251.  
  252.   new_bloc->size = size;
  253.   new_bloc->next = NIL_BLOC;
  254.   new_bloc->variable = (POINTER *) NIL;
  255.  
  256.   if (first_bloc)
  257.     {
  258.       new_bloc->prev = last_bloc;
  259.       last_bloc->next = new_bloc;
  260.       last_bloc = new_bloc;
  261.     }
  262.   else
  263.     {
  264.       first_bloc = last_bloc = new_bloc;
  265.       new_bloc->prev = NIL_BLOC;
  266.     }
  267.  
  268.   return new_bloc;
  269. }
  270.  
  271. /* Relocate all blocs from BLOC on upward in the list to the zone
  272.    indicated by ADDRESS.  Direction of relocation is determined by
  273.    the position of ADDRESS relative to BLOC->data.
  274.  
  275.    If BLOC is NIL_BLOC, nothing is done.
  276.  
  277.    Note that ordering of blocs is not affected by this function. */
  278.  
  279. static void
  280. relocate_some_blocs (bloc, address)
  281.      bloc_ptr bloc;
  282.      POINTER address;
  283. {
  284.   if (bloc != NIL_BLOC)
  285.     {
  286.       register SIZE offset = address - bloc->data;
  287.       register SIZE data_size = 0;
  288.       register bloc_ptr b;
  289.       
  290.       for (b = bloc; b != NIL_BLOC; b = b->next)
  291.     {
  292.       data_size += b->size;
  293.       b->data += offset;
  294.       *b->variable = b->data;
  295.     }
  296.  
  297.       safe_bcopy (address - offset, address, data_size);
  298.     }
  299. }
  300.  
  301.  
  302. /* Free BLOC from the chain of blocs, relocating any blocs above it
  303.    and returning BLOC->size bytes to the free area. */
  304.  
  305. static void
  306. free_bloc (bloc)
  307.      bloc_ptr bloc;
  308. {
  309.   if (bloc == first_bloc && bloc == last_bloc)
  310.     {
  311.       first_bloc = last_bloc = NIL_BLOC;
  312.     }
  313.   else if (bloc == last_bloc)
  314.     {
  315.       last_bloc = bloc->prev;
  316.       last_bloc->next = NIL_BLOC;
  317.     }
  318.   else if (bloc == first_bloc)
  319.     {
  320.       first_bloc = bloc->next;
  321.       first_bloc->prev = NIL_BLOC;
  322.     }
  323.   else
  324.     {
  325.       bloc->next->prev = bloc->prev;
  326.       bloc->prev->next = bloc->next;
  327.     }
  328.  
  329.   relocate_some_blocs (bloc->next, bloc->data);
  330.   relinquish (bloc->size);
  331.   free (bloc);
  332. }
  333.  
  334. /* Interface routines.  */
  335.  
  336. static int use_relocatable_buffers;
  337.  
  338. /* Obtain SIZE bytes of storage from the free pool, or the system, as
  339.    necessary.  If relocatable blocs are in use, this means relocating
  340.    them.  This function gets plugged into the GNU malloc's __morecore
  341.    hook.
  342.  
  343.    We provide hysteresis, never relocating by less than extra_bytes.
  344.  
  345.    If we're out of memory, we should return zero, to imitate the other
  346.    __morecore hook values - in particular, __default_morecore in the
  347.    GNU malloc package.  */
  348.  
  349. POINTER 
  350. r_alloc_sbrk (size)
  351.      long size;
  352. {
  353.   /* This is the first address not currently available for the heap.  */
  354.   POINTER top;
  355.   /* Amount of empty space below that.  */
  356.   /* It is not correct to use SIZE here, because that is usually unsigned.
  357.      ptrdiff_t would be okay, but is not always available.
  358.      `long' will work in all cases, in practice.  */
  359.   long already_available;
  360.   POINTER ptr;
  361.  
  362.   if (! use_relocatable_buffers)
  363.     return (*real_morecore) (size);
  364.  
  365.   top = first_bloc ? first_bloc->data : page_break_value;
  366.   already_available = (char *) top - (char *) virtual_break_value;
  367.  
  368.   /* Do we not have enough gap already?  */
  369.   if (size > 0 && already_available < size)
  370.     {
  371.       /* Get what we need, plus some extra so we can come here less often.  */
  372.       SIZE get = size - already_available + extra_bytes;
  373.  
  374.       if (! obtain (get))
  375.     return 0;
  376.  
  377.       if (first_bloc)
  378.     relocate_some_blocs (first_bloc, first_bloc->data + get);
  379.  
  380.       /* Zero out the space we just allocated, to help catch bugs
  381.      quickly.  */
  382.       bzero (virtual_break_value, get);
  383.     }
  384.   /* Can we keep extra_bytes of gap while freeing at least extra_bytes?  */
  385.   else if (size < 0 && already_available - size > 2 * extra_bytes)
  386.     {
  387.       /* Ok, do so.  This is how many to free.  */
  388.       SIZE give_back = already_available - size - extra_bytes;
  389.  
  390.       if (first_bloc)
  391.     relocate_some_blocs (first_bloc, first_bloc->data - give_back);
  392.       relinquish (give_back);
  393.     }
  394.  
  395.   ptr = virtual_break_value;
  396.   virtual_break_value += size;
  397.  
  398.   return ptr;
  399. }
  400.  
  401. /* Allocate a relocatable bloc of storage of size SIZE.  A pointer to
  402.    the data is returned in *PTR.  PTR is thus the address of some variable
  403.    which will use the data area.
  404.  
  405.    If we can't allocate the necessary memory, set *PTR to zero, and
  406.    return zero.  */
  407.  
  408. POINTER
  409. r_alloc (ptr, size)
  410.      POINTER *ptr;
  411.      SIZE size;
  412. {
  413.   register bloc_ptr new_bloc;
  414.  
  415.   if (! r_alloc_initialized)
  416.     r_alloc_init ();
  417.  
  418.   new_bloc = get_bloc (size);
  419.   if (new_bloc)
  420.     {
  421.       new_bloc->variable = ptr;
  422.       *ptr = new_bloc->data;
  423.     }
  424.   else
  425.     *ptr = 0;
  426.  
  427.   return *ptr;
  428. }
  429.  
  430. /* Free a bloc of relocatable storage whose data is pointed to by PTR.
  431.    Store 0 in *PTR to show there's no block allocated.  */
  432.  
  433. void
  434. r_alloc_free (ptr)
  435.      register POINTER *ptr;
  436. {
  437.   register bloc_ptr dead_bloc;
  438.  
  439.   dead_bloc = find_bloc (ptr);
  440.   if (dead_bloc == NIL_BLOC)
  441.     abort ();
  442.  
  443.   free_bloc (dead_bloc);
  444.   *ptr = 0;
  445. }
  446.  
  447. /* Given a pointer at address PTR to relocatable data, resize it to SIZE.
  448.    Do this by shifting all blocks above this one up in memory, unless
  449.    SIZE is less than or equal to the current bloc size, in which case
  450.    do nothing.
  451.  
  452.    Change *PTR to reflect the new bloc, and return this value.
  453.  
  454.    If more memory cannot be allocated, then leave *PTR unchanged, and
  455.    return zero.  */
  456.  
  457. POINTER
  458. r_re_alloc (ptr, size)
  459.      POINTER *ptr;
  460.      SIZE size;
  461. {
  462.   register bloc_ptr bloc;
  463.  
  464.   bloc = find_bloc (ptr);
  465.   if (bloc == NIL_BLOC)
  466.     abort ();
  467.  
  468.   if (size <= bloc->size)
  469.     /* Wouldn't it be useful to actually resize the bloc here?  */
  470.     return *ptr;
  471.  
  472.   if (! obtain (size - bloc->size))
  473.     return 0;
  474.  
  475.   relocate_some_blocs (bloc->next, bloc->data + size);
  476.  
  477.   /* Zero out the new space in the bloc, to help catch bugs faster.  */
  478.   bzero (bloc->data + bloc->size, size - bloc->size);
  479.  
  480.   /* Indicate that this block has a new size.  */
  481.   bloc->size = size;
  482.  
  483.   return *ptr;
  484. }
  485.  
  486. /* The hook `malloc' uses for the function which gets more space
  487.    from the system.  */
  488. extern POINTER (*__morecore) ();
  489.  
  490. /* Intialize various things for memory allocation. */
  491.  
  492. static void
  493. r_alloc_init ()
  494. {
  495.   if (r_alloc_initialized)
  496.     return;
  497.  
  498.   r_alloc_initialized = 1;
  499.   real_morecore = __morecore;
  500.   __morecore = r_alloc_sbrk;
  501.  
  502.   virtual_break_value = break_value = (*real_morecore) (0);
  503.   if (break_value == NIL)
  504.     abort ();
  505.  
  506.   page_size = PAGE;
  507.   extra_bytes = ROUNDUP (50000);
  508.  
  509.   page_break_value = (POINTER) ROUNDUP (break_value);
  510.   /* Clear the rest of the last page; this memory is in our address space
  511.      even though it is after the sbrk value.  */
  512.   bzero (break_value, (page_break_value - break_value));
  513.   use_relocatable_buffers = 1;
  514. }
  515.